const personal = require('../../../../api/personal.js')
Page({
  data: {
    areaList: {},
    loading: true,
    value: 330302,
    show: false,
    name: '',
    phone: '',
    address: '',
    address_detail: '',
    id: '',
    isSelected: null,
    userInfo: {}
  },
  onLoad(options) {
    if(options.id) {
      this.setData({
        id: options.id
      })
    }
  },
  // 获取用户信息
  getUserInfo(token) {
    personal.getUserInfo(token).then(res => {
      if (res.data.code == 0) {
        this.setData({
          userInfo: res.data.data
        })
        if (this.data.id) {
          let address = wx.getStorageSync('address' + this.data.userInfo.id) || []
          address.forEach(value => {
            if (value.id == this.data.id) {
              this.setData({
                name: value.name,
                phone: value.phone,
                address: value.address,
                address_detail: value.address_detail,
                isSelected: value.isSelected
              })
            }
          })
        }
        wx.request({
          url: 'https://cashier.youzan.com/wsctrade/uic/address/getAllRegion.json',
          success: response => {
            this.setData({
              loading: false,
              areaList: response.data.data
            });
          }
        })
        wx.hideLoading()
      }
    })
  },
  onShow() {
    let token = wx.getStorageSync('token')
    this.getUserInfo(token)
  },
  onChange(event) {
    const {
      values
    } = event.detail;
  },
  onConfirm(event) {
    const {
      values
    } = event.detail
    let name = ''
    values.forEach(value => {
      name += value.name + ' '
    })
    this.setData({
      address: name,
      show: false
    })
  },
  onCancel(event) {
    this.setData({
      show: false
    })
  },
  areaShow(e) {
    this.setData({
      show: !this.data.show
    })
  },
  onChangeName(e) {
    this.setData({
      name: e.detail
    })
  },
  onChangePhone(e) {
    this.setData({
      phone: e.detail
    })
  },
  onChangeAddress(e) {
    this.setData({
      address_detail: e.detail
    })
  },
  // 保存地址
  onSubmit() {
    let address = wx.getStorageSync('address' + this.data.userInfo.id) || []
    let re = /^1\d{10}$/
    if (!re.test(this.data.phone)) {
      wx.showToast({
        title: '手机格式不正确',
        icon: 'none'
      })
      return
    }
    let obj = {
      name: this.data.name,
      phone: this.data.phone,
      address: this.data.address,
      address_detail: this.data.address_detail
    }
    if (this.data.id) {
      console.log(this.data.id)
      obj.isSelected = this.data.isSelected
      address.forEach(value => {
        if(value.id == this.data.id) {
          obj.id = Number(this.data.id)
          value.id = obj.id
          value.name = obj.name
          value.phone = obj.phone
          value.address = obj.address
          value.address_detail = obj.address_detail
        }
      })
      wx.setStorageSync('address' + this.data.userInfo.id, address)
    } else {
      obj.isSelected = false
      address.push(obj)
      address.forEach((value, index) => {
        value.id = index
      })
      wx.setStorageSync('address' + this.data.userInfo.id, address)
    }
    wx.showToast({
      title: '保存成功',
    })
    wx.navigateBack({
      delta: 1
    })
  }
});